package cn.wolfcode.luowowo.search.service.impl;

import cn.wolfcode.luowowo.search.repository.IDestinationCommendRepository;
import cn.wolfcode.luowowo.serach.query.DestTemeSearchQueryObject;
import cn.wolfcode.luowowo.serach.service.IDestinationCommendSearchService;
import cn.wolfcode.luowowo.serach.template.DestinationCommendTemplate;
import com.alibaba.dubbo.config.annotation.Service;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;

import java.util.List;

/**
 * Created by admin on 2019/8/17.
 */
@Service
public class DestinationCommendSearchServiceImpl implements IDestinationCommendSearchService {

    @Autowired
    private IDestinationCommendRepository repository;

    @Autowired
    private ElasticsearchTemplate template;

    @Override
    public void saveOrUpdate(DestinationCommendTemplate destinationCommendTemplate) {
        repository.save(destinationCommendTemplate);
    }

    @Override
    public List<DestinationCommendTemplate> getDestByMonthId(int monthId) {

        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        builder.withQuery(QueryBuilders.termQuery("month", monthId));
        builder.withPageable(PageRequest.of(0, 4));
        List<DestinationCommendTemplate> templates = template.queryForList(builder.build(), DestinationCommendTemplate.class);
        return templates;
    }

    @Override
    public List<DestinationCommendTemplate> selectThemeById(Long id) {
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        builder.withQuery(QueryBuilders.termQuery("destThemeType", id));
        List<DestinationCommendTemplate> templates = template.queryForList(builder.build(), DestinationCommendTemplate.class);
        return templates;
    }

    @Override
    public Page query(DestTemeSearchQueryObject qo) {


//        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
//        if (qo.getMid() != null) {
//            boolQuery.should(QueryBuilders.matchQuery("month", qo.getMid()));
//        } else if (qo.getDid() != null) {
//            boolQuery.should(QueryBuilders.rangeQuery("days").lte(qo.getDays().getMax()).gte(qo.getDays().getMin()));
//        } else if (qo.getTid() != null) {
//            boolQuery.should(QueryBuilders.matchQuery("destThemeId", qo.getTid()));
//        }


        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();

        if (qo.getMid() != null) {
            boolQueryBuilder.filter(QueryBuilders.termQuery("month", qo.getMid()));
        } else if (qo.getDid() != null) {
            boolQueryBuilder.filter(QueryBuilders.rangeQuery("days").lte(qo.getDays().getMax()).gte(qo.getDays().getMin()));
        } else if (qo.getTid() != null) {
            boolQueryBuilder.filter(QueryBuilders.termQuery("destThemeId", qo.getTid()));
        }


        /*if (qo.getMonth() != -1) {
            boolQueryBuilder.filter(QueryBuilders.termQuery("month", qo.getMonth()));
        }
        if (qo.getThemeId() != -1) {
            boolQueryBuilder.filter(QueryBuilders.termQuery("themeId", qo.getThemeId()));
        }

        boolQueryBuilder.filter(QueryBuilders.rangeQuery("days").gte(minDays).lte(maxDays));

        */


        NativeSearchQuery query = new NativeSearchQuery(boolQueryBuilder);
        query.addIndices(DestinationCommendTemplate.INDEX_NAME);
        query.addTypes(DestinationCommendTemplate.TYPE_NAME);
        Page<DestinationCommendTemplate> page = repository.search(query.getQuery(),
                PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize()));
        return page;
    }
}